<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\Circle;
use App\Models\CircleCategory;
use App\Models\Message;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class CircleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Circle(['user:id,nickname,avatar','category:id,name']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('logo')->image('',80,80);
            $grid->column('name');
            $grid->column('area');
            $grid->column('category.name','圈子类型');
            $grid->column('audit_status')
                ->using([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->fixColumns(-1);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Circle(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('area');
            $show->field('intro');
            $show->field('circle_category_id');
            $show->field('logo');
            $show->field('bg_image');
            $show->field('firm_image');
            $show->field('business_license');
            $show->field('audit_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Circle(), function (Form $form) {
            $form->display('id');
            $form->selectTable('user_id','用户')
                ->from(UserTable::make())
                ->model(User::class,'id','nickname')
                ->saving(function ($value){
                    return (int)$value;
                });
            $form->image('logo')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->required();
            $form->text('name')
                 ->maxLength(60)
                 ->required();
            $form->select('circle_category_id')
                ->options(CircleCategory::getOption())
                ->required();
            $form->image('bg_image')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->required();
            $form->text('area')
                 ->maxLength(100)
                 ->required();
            $form->textarea('intro')
                 ->required();
            $form->radio('audit_status')
                ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->when(3,function (Form $form){
                    $form->textarea('reject_reason','拒绝原因')
                        ->saving(function ($value){
                            return (string)$value;
                        })->rules('max:255',['max' => '最多500字']);
                })->required();

            $form->saving(function (Form $form){
                if ($form->input('audit_status')==2){
                    if ($form->input('user_id')){
                        $auth = DB::table('users')->where('id',$form->input('user_id'))->value('auth');

                        if ($auth==1){
                            $name = DB::table('user_auths')->where('user_id',$form->input('user_id'))->value('name');
                        }else{
                            $name = DB::table('firms')->where('user_id',$form->input('user_id'))->value('corporate_name');
                        }

                        if ($name){
                            Message::query()->create([
                                'type' => 5,
                                'content' => $name. '创建了新圈子'
                            ]);
                        }
                    }
                }

            });


            $form->saved(function (Form $form){
                if ($form->input('user_id') && $form->input('audit_status')==2){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '恭喜您发布的圈子已通过审核'
                    ]));
                }

                if ($form->input('user_id') && $form->input('audit_status')==3){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '您发布的圈子未通过审核'
                    ]));
                }

            });





            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
